package com.example.sztangli.widgedemo.RecycleListView;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;

import com.example.sztangli.widgedemo.R;
import com.example.sztangli.widgedemo.base.BaseFragment;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by sztangli on 2017/8/15.
 */

public class RecycleFragment extends BaseFragment implements View.OnClickListener {
    @BindView(R.id.recycleView)
    RecyclerView recyclerView;
    @BindView(R.id.linearMode_horizontal)
    Button linearH;
    @BindView(R.id.linearMode_vertical)
    Button linearV;
    @BindView(R.id.gridMode_horizontal)
    Button gridH;
    @BindView(R.id.gridMode_vertical)
    Button gridV;
    @BindView(R.id.staggeredgrid)
    Button staggeredgrid;
    public static final String TAG = "RecycleFragment";
    private Context context;

    public static RecycleFragment newInstance() {

        Bundle args = new Bundle();

        RecycleFragment fragment = new RecycleFragment();
        fragment.setArguments(args);
        return fragment;
    }
    @Override
    public int getLayoutResId() {
        return R.layout.fragment_recycleview;
    }

    @Override
    public void creatView() {
        context=getContext();
      recyclerView.setLayoutManager(new StaggeredGridLayoutManager(4,StaggeredGridLayoutManager.VERTICAL));
      recyclerView.addItemDecoration(new GridDecoration(context));
      recyclerView.setAdapter(new RecyclViewAdapter(context,getDatas()));
        linearH.setOnClickListener(this);
        linearV.setOnClickListener(this);
        gridH.setOnClickListener(this);
        gridV.setOnClickListener(this);
        staggeredgrid.setOnClickListener(this);
    }

    private List<RecycleBean> getDatas(){
        List<RecycleBean> datas=new ArrayList<>();
        for(int i=0;i<10;i++){
            RecycleBean bean=new RecycleBean();
            bean.setContent("data "+i);
            datas.add(bean);
        }
        return datas;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.linearMode_horizontal:
                recyclerView.setLayoutManager(new LinearLayoutManager(context,LinearLayoutManager.HORIZONTAL,false));
                recyclerView.addItemDecoration(new LinearDecoration(context, LinearLayout.HORIZONTAL));
                recyclerView.setAdapter(new RecyclViewAdapter(context,getDatas()));
                recyclerView.invalidate();
                break;
            case R.id.linearMode_vertical:
                recyclerView.setLayoutManager(new LinearLayoutManager(context));
                recyclerView.addItemDecoration(new LinearDecoration(context,LinearLayout.VERTICAL));
                recyclerView.setAdapter(new RecyclViewAdapter(context,getDatas()));
                recyclerView.invalidate();
                break;
            case R.id.gridMode_horizontal:
                recyclerView.setLayoutManager(new GridLayoutManager(context,4,GridLayoutManager.HORIZONTAL,false));
                recyclerView.addItemDecoration(new GridDecoration(context));
                recyclerView.setAdapter(new RecyclViewAdapter(context,getDatas()));
                recyclerView.invalidate();
                break;
            case R.id.gridMode_vertical:
                recyclerView.setLayoutManager(new GridLayoutManager(context,4));
                recyclerView.addItemDecoration(new GridDecoration(context));
                recyclerView.setAdapter(new RecyclViewAdapter(context,getDatas()));
                recyclerView.invalidate();
                break;
            case R.id.staggeredgrid:
                recyclerView.setLayoutManager(new StaggeredGridLayoutManager(4,StaggeredGridLayoutManager.VERTICAL));
                recyclerView.addItemDecoration(new GridDecoration(context));
                recyclerView.setAdapter(new RecyclViewAdapter(context,getDatas()));
                recyclerView.invalidate();
                break;
        }
    }
}
